<?php
/**
* Search Model Class
*
* Section operations
* 
* @author EAE
* @link searchmodel
*/ 

class Searchmodel extends Model {

    function __construct()
    {
        parent::Model();
    }
    
    public function search_products($searchword){

         $this->db->select('products.id_Products AS id, Name_Products AS title, CONCAT(Introduction_Products, Description_Products) AS text, Main_Image AS image', FALSE)
                ->from('products')   
                ->join('categories', 'products.id_Categories = categories.id_Categories', 'left')
                ->where('Active_Products', 1)    
                ->where('Active_Categories', 1)    
                ->where('MATCH (Name_Products, Introduction_Products, Description_Products) AGAINST ("'.$searchword.'")', NULL, FALSE);

        $query = $this->db->get();    

        if ($query != NULL) {
            return $query->result_array();
        } else {
            return FALSE;
        }

    }
    public function search_product_categories($searchword){

         $this->db->select('categories.id_Categories AS id, Title_Categories AS title, CONCAT(Introduction_Categories, Description_Categories) AS text, Image_Categories AS image, sum(Active_Products) AS active_product_counter', FALSE)
                ->from('categories')   
                ->join('products', 'categories.id_Categories = products.id_Categories', 'left')   
                ->where('Active_Categories', 1)    
                ->where('MATCH (Title_Categories, Introduction_Categories, Description_Categories) AGAINST ("'.$searchword.'")', NULL, FALSE)
                ->group_by('categories.id_Categories');

        $query = $this->db->get();                                     

        if ($query != NULL) {
            $result = $query->result_array();
            // remove subcategories that don't have active products
            foreach($result as $key=>&$cat){  
                if (!$cat["active_product_counter"]>0) unset($result[$key]);         
            }
            return $result;
        } else {
            return FALSE;
        }

    }
    
    public function search_news($searchword){

         $this->db->select('id_News As id, Date_News AS date, Title_News AS title, CONCAT(Introduction_News, Description_News) AS text', FALSE)
                ->from('news')                                                           
                ->where('Active_News', 1)
                ->where('MATCH (Title_News, Introduction_News, Description_News) AGAINST ("'.$searchword.'")', NULL, FALSE);
        
        $query = $this->db->get();    
  
        $query->result_array(); 

        return $query->result_array();

    }
    
    public function search_publicity($searchword){

         $this->db->select('id_Publicity AS id, Date_Publicity As date, Title_Publicity AS title, CONCAT(Introduction_Publicity, Description_Publicity) AS text', FALSE)
                ->from('publicity')                                                           
                ->where('Active_Publicity', 1)
                ->where('MATCH (Title_Publicity, Introduction_Publicity, Description_Publicity) AGAINST ("'.$searchword.'")', NULL, FALSE);
        
        $query = $this->db->get();    
  
        $query->result_array(); 

        return $query->result_array();

    }

    public function search_portfolio($searchword){

         $this->db->select('Name_Portfolio AS title, CONCAT(Introduction_Portfolio, Description_Portfolio) AS text, id_Portfolio AS id, Main_Image AS image', FALSE)
                ->from('portfolio')                                                           
                ->where('Active_Portfolio', 1)
                ->where('MATCH (Name_Portfolio, Introduction_Portfolio, Description_Portfolio) AGAINST ("'.$searchword.'")', NULL, FALSE);
        
        $query = $this->db->get();    
            
        return $query->result_array();

    }
    
    public function search_glossary($searchword){

         $this->db->select('Title_Glossary AS title, CONCAT(Introduction_Glossary, Description_Glossary) AS text, id_Glossary As id', FALSE)
                ->from('glossary')                                                           
                ->where('Active_Glossary', 1)
                ->where('MATCH (Title_Glossary, Introduction_Glossary, Description_Glossary) AGAINST ("'.$searchword.'")', NULL, FALSE);
        
        $query = $this->db->get();    
  
        $query->result_array(); 

        return $query->result_array();

    }
    
    public function search_articles($searchword){

         $this->db->select('id_Articles As id, Date_Articles AS date, Title_Articles AS title, CONCAT(Introduction_Articles, Description_Articles) As text', FALSE)
                ->from('articles')                                                           
                ->where('Active_Articles', 1)
                ->where('MATCH (Title_Articles, Introduction_Articles, Description_Articles) AGAINST ("'.$searchword.'")', NULL, FALSE);
        
        $query = $this->db->get();    
  
        $query->result_array(); 

        return $query->result_array();

    }
    
    public function search_exhibitions($searchword){

         $this->db->select('id_Exhibitions As id, Date_Exhibitions AS date, Title_Exhibitions AS title, CONCAT(Introduction_Exhibitions, Description_Exhibitions) AS text')
                ->from('exhibitions')                                                           
                ->where('Active_Exhibitions', 1)
                ->where('MATCH (Title_Exhibitions, Introduction_Exhibitions, Description_Exhibitions) AGAINST ("'.$searchword.'")', NULL, FALSE);
        
        $query = $this->db->get();    
  
        $query->result_array(); 

        return $query->result_array();

    }

   
}



    
    
/* End of file searchmodel.php */
/* Location: ./system/application/models/searchmodel.php */ 
?>